package com.hnkywl.sts.service.htgl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.htgl.CghtDao;
import com.hnkywl.sts.dao.htgl.XshtDao;
import com.hnkywl.sts.dao.jhgl.BgjhDao;
import com.hnkywl.sts.dao.jhgl.CgwzjhDao;
import com.hnkywl.sts.dao.organ.FkfsDao;
import com.hnkywl.sts.dao.organ.LjwhDao;
import com.hnkywl.sts.dao.xsgl.XsdDao;
import com.hnkywl.sts.dao.zjgl.JzjsDao;
import com.hnkywl.sts.entity.djgl.Jydd;
import com.hnkywl.sts.entity.htgl.Cght;
import com.hnkywl.sts.entity.htgl.Fkfs;
import com.hnkywl.sts.entity.htgl.Htmx;
import com.hnkywl.sts.entity.htgl.Xsht;
import com.hnkywl.sts.entity.jhgl.Bgjh;
import com.hnkywl.sts.entity.jhgl.Cgwzjh;
import com.hnkywl.sts.entity.jhgl.Zgwzjh;
import com.hnkywl.sts.entity.organ.Lj;
import com.hnkywl.sts.entity.xsgl.Xsd;
import com.hnkywl.sts.entity.xsgl.Xsmx;
import com.hnkywl.sts.entity.zjgl.Jzjs;
import com.hnkywl.sts.service.base.BmService;
import com.hnkywl.sts.service.base.SjzdzService;
import com.hnkywl.sts.service.kcgl.CkshService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.CalcUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-4-8
 * Time: 下午1:44
 * To change this template use File | Settings | File Templates.
 */
@Service
public class XshtService {
    private XshtDao xshtDao;
    private JzjsDao jzjsDao;
    private XsdDao xsdDao;

    private CghtDao cghtDao;

    private SjzdzService sjzdzService;

    private CgwzjhDao cgwzjhDao;

    private BmService bmService;

    private LjwhDao ljwhDao;

    private BgjhDao bgjhDao;

    private FkfsDao fkfsDao;

    private CkshService ckshService;

    /**
     * 查询销售合同
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public String findAllXsHt(String zt, String xslx, String htbh, Long ssgsId, Long ssbmId, Long xsrId, Long hzfId, Date startDate, Date endDate, String htbm, int pageNo, int pageSize) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        Page<Xsht> xshtPage = this.xshtDao.findXsHtList(zt, xslx, htbh, ssgsId, ssbmId, xsrId, hzfId, startDate, endDate, htbm, pageNo, pageSize);
        String xshtJson = gson.toJson(xshtPage.getResult());
        return "{\"total\":" + xshtPage.getTotalCount() + ",\"rows\":" + xshtJson + "}";
    }

    /**
     * 查询销售合同
     *
     * @return
     */
    public String findAllSimpleXsHt(String zt, String khmc, int pageNo, int pageSize) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        Page xshtPage = this.xshtDao.findXsHtList(zt, khmc, pageNo, pageSize);
        String xshtJson = gson.toJson(xshtPage.getResult());
        return "{\"total\":" + xshtPage.getTotalCount() + ",\"rows\":" + xshtJson + "}";
    }


    /**
     * 销售合同统计
     *
     * @param zt
     * @param fhrqStart
     * @param xhtbm
     * @param fhrqEnd
     * @param pageNo
     * @param pageSize  @return
     */
    public String findAllXsHtTj(String htbm, String xhtbm, Long khId, Date fhrqStart, Date fhrqEnd, String zt, int pageNo, int pageSize) {
        Gson gson = new Gson();
        Page<Map> xshtPage = this.xshtDao.findAllXsHtTj(htbm, xhtbm, khId, fhrqStart, fhrqEnd, zt, pageNo, pageSize);
        String json = gson.toJson(xshtPage.getResult());
        return "{\"total\":" + xshtPage.getTotalCount() + ",\"rows\":" + json + "}";
    }


    /**
     * 查询采销售同明细
     *
     * @param xshtId 销售合同ID
     * @return
     */
    public Xsht findXshtDetail(Long xshtId) {
        return this.xshtDao.get(xshtId);
    }

    /**
     * 查询销售合同明细
     *
     * @param processInstanceId 协议ID
     * @return
     */
    @Transactional
    public Xsht findXshtDetailByProcessInstance(String processInstanceId) {
        return this.xshtDao.findUniqueByProperty("processInstanceId", processInstanceId);
    }


    /**
     * 通过直达到货的销售合同生成销售合同
     *
     * @param cght
     */
    @Transactional
    public void createXsht(Cght cght) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, SQLException {
        //根据销售合同里的计划汇总的ID找到计划的List
        List<Bgjh> bgjhs = bgjhDao.findBgjhListByBgjhhzId(cght.getBgjhhz().getId());
        //根据计划遍历建销售合同
        for (Bgjh bgjh : bgjhs) {
            Lj lj = ljwhDao.get(bgjh.getLj().getId());
            Xsht xsht = new Xsht();
            xsht.setZt("ybc");
            xsht.setXslx("kcxs");
            xsht.setZddhcght(cght);
            xsht.setLj(bgjh.getLj());
            xsht.setQdrq(cght.getQdrq());
            xsht.setJsrq(new Date());
            xsht.setJsyj(lj.getJsfs());
            xsht.setQddd(cght.getQddd());
            xsht.setJhdd(cght.getJhdd());
            xsht.setHzf(bgjh.getJhdw());
            //xsht.setWlfgs(YgService.getCurrentYg().getZgjg());
            xsht.setWlfgs(YgService.getCurrentYg().getZggs());
            xsht.setHtbm(this.bmService.getHtbm("XSHT"));
            xsht.setXsxt_htbm(cght.getXsxt_htbm());
            // xsht.setSbgs(YgService.getCurrentYg().getZgjg());
            xsht.setSbgs(YgService.getCurrentYg().getZggs());
            xsht.setSbr(YgService.getCurrentYg());
            xsht.setCreateUser(YgService.getCurrentYg());
            xsht.setCreateOrgan(YgService.getCurrentYg().getZggs());
            xsht.setCreateDepartment(YgService.getCurrentYg().getZgjg());
            xsht.setLastUpdated(new Date());
            xsht.setSl(17.0000);
            xsht.setHkqx(Integer.parseInt(lj.getHkq()));
            xsht.setXsfy(sjzdzService.findSjzdzByMc("无费用"));
            xsht.setHtxz(sjzdzService.findSjzdzByMc("标准合同"));
            xsht.setFjList(null);
            xsht.setMxts(cght.getMxts());
            xsht.setTxl(cght.getTxl());
            xsht.setHtms("xxms");
            //合同明细
            List<Htmx> xshtmxList = new ArrayList<Htmx>();
            if (bgjh.getBgjhmxList().size() != 0) {
                for (int i = 0; i < bgjh.getBgjhmxList().size(); i++) {
                    Htmx xshtmx = new Htmx();
                    xshtmx.setWzgg(bgjh.getBgjhmxList().get(i).getWzgg());
                    xshtmx.setSbsl(bgjh.getBgjhmxList().get(i).getJhsl());
                    xshtmx.setSbjs(bgjh.getBgjhmxList().get(i).getJhjs());
                    xshtmx.setCd(cght.getHtmxList().get(i).getCd());
                    xshtmx.setJzfs(cght.getHtmxList().get(i).getJzfs());
                    xshtmx.setPh(bgjh.getBgjhmxList().get(i).getPh());
                    xshtmx.setXsxt_xsdh(cght.getHtmxList().get(i).getXsxt_xsdh());
                    xshtmx.setSpec(cght.getHtmxList().get(i).getSpec());
                    xshtmx.setMxh(Long.valueOf(i + 1));
                    xshtmx.setCreateUser(YgService.getCurrentYg());
                    xshtmx.setCreateOrgan(YgService.getCurrentYg().getZggs());
                    xshtmx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                    xshtmx.setLastUpdated(new Date());
                    xshtmx.setZt("ybc");
                    xshtmx.setBhsj(CalcUtils.round(cght.getHtmxList().get(i).getBhsj(), 4));
                    xshtmx.setHsj(CalcUtils.round(cght.getHtmxList().get(i).getHsj(), 4));
                    xshtmx.setHsje(CalcUtils.round(cght.getHtmxList().get(i).getHsje(), 2));
                    xshtmxList.add(xshtmx);
                }
            }
            xsht.setHtmxList(xshtmxList);
            //根据销售合同的客户找到对应的付款方式带入
            List<Fkfs> fkfses = fkfsDao.findAllFkfsListByQyjgId(xsht.getHzf().getId());
            List<Fkfs> fkfses1 = new ArrayList<Fkfs>();
            if (fkfses.size() != 0) {
                for (Fkfs fkfs : fkfses) {
                    Fkfs f = new Fkfs();
                    f = (Fkfs) ReflectionUtils.getInstance(f, fkfs);
                    f.setId(null);
                    f.setQyjg(null);
                    fkfses1.add(f);
                }
            }
            xsht.setHtfkfs(fkfses1);
            xshtDao.save(xsht);
        }
    }

    @Transactional
    public void createXsht2(Cght cght) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, SQLException {
        //根据销售合同里的计划汇总的ID找到计划的List
        //   List<Bgjh> bgjhs = bgjhDao.findBgjhListByBgjhhzId(cght.getBgjhhz().getId());
        //根据计划遍历建销售合同
        // for (Bgjh bgjh : bgjhs) {
        Lj lj = ljwhDao.get(cght.getLj().getId());
        Xsht xsht = new Xsht();
        xsht.setZt("ybc");
        xsht.setXslx("kcxs");
        xsht.setZddhcght(cght);
        xsht.setLj(cght.getLj());
        xsht.setQdrq(cght.getQdrq());
        xsht.setJsrq(new Date());
        xsht.setJsyj(lj.getJsfs());
        xsht.setQddd(cght.getQddd());
        xsht.setJhdd(cght.getJhdd());
        xsht.setHzf(cght.getHzf());
        xsht.setWlfgs(YgService.getCurrentYg().getZgjg());
        xsht.setHtbm(this.bmService.getHtbm("XSHT"));
        xsht.setXsxt_htbm(cght.getXsxt_htbm());
        xsht.setSbgs(YgService.getCurrentYg().getZgjg());
        xsht.setSbr(YgService.getCurrentYg());
        xsht.setCreateUser(YgService.getCurrentYg());
        xsht.setCreateOrgan(YgService.getCurrentYg().getZggs());
        xsht.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        xsht.setLastUpdated(new Date());
        xsht.setSl(17.0000);
        xsht.setHkqx(Integer.parseInt(lj.getHkq()));
        xsht.setXsfy(sjzdzService.findSjzdzByMc("无费用"));
        xsht.setHtxz(sjzdzService.findSjzdzByMc("标准合同"));
        xsht.setFjList(null);
        xsht.setMxts(cght.getMxts());
        xsht.setTxl(cght.getTxl());
        xsht.setHtms("xxms");
        //合同明细


        List<Htmx> htmxList = cght.getHtmxList();
        List<Htmx> xshtmxList = new ArrayList<Htmx>();
        if (htmxList != null) {
            // if (xsht.getHtmxList().size() != 0) {
            for (int i = 0; i < htmxList.size(); i++) {
                Htmx xshtmx = new Htmx();
                xshtmx.setWzgg(cght.getHtmxList().get(i).getWzgg());
                xshtmx.setSbsl(cght.getHtmxList().get(i).getSbsl());
                xshtmx.setSbjs(cght.getHtmxList().get(i).getSbjs());
                xshtmx.setCd(cght.getHtmxList().get(i).getCd());
                xshtmx.setJzfs(cght.getHtmxList().get(i).getJzfs());
                xshtmx.setPh(cght.getHtmxList().get(i).getPh());
                xshtmx.setXsxt_xsdh(cght.getHtmxList().get(i).getXsxt_xsdh());
                xshtmx.setSpec(cght.getHtmxList().get(i).getSpec());
                xshtmx.setMxh(Long.valueOf(i + 1));
                xshtmx.setCreateUser(YgService.getCurrentYg());
                xshtmx.setCreateOrgan(YgService.getCurrentYg().getZggs());
                xshtmx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                xshtmx.setLastUpdated(new Date());
                xshtmx.setZt("ybc");
                xshtmx.setBhsj(CalcUtils.round(cght.getHtmxList().get(i).getBhsj(), 4));
                xshtmx.setHsj(CalcUtils.round(cght.getHtmxList().get(i).getHsj(), 4));
                xshtmx.setHsje(CalcUtils.round(cght.getHtmxList().get(i).getHsje(), 2));
                xshtmxList.add(xshtmx);
            }
        }
        xsht.setHtmxList(xshtmxList);
        //根据销售合同的客户找到对应的付款方式带入
        List<Fkfs> fkfses = fkfsDao.findAllFkfsListByQyjgId(xsht.getHzf().getId());
        List<Fkfs> fkfses1 = new ArrayList<Fkfs>();
        if (fkfses.size() != 0) {
            for (Fkfs fkfs : fkfses) {
                Fkfs f = new Fkfs();
                f = (Fkfs) ReflectionUtils.getInstance(f, fkfs);
                f.setId(null);
                f.setQyjg(null);
                fkfses1.add(f);
            }
        }
        xsht.setHtfkfs(fkfses1);
        xshtDao.save(xsht);

    }

    /**
     * 查询销售合同明细
     *
     * @param xshtId 售合合同ID
     * @return
     */
    public String findXshtmxDetail(Long xshtId, int pageNo, int pageSize) {
        if (xshtId != null) {
            Gson gson = new Gson();
            Page<Map> page = this.xshtDao.findXshtmxList(xshtId, pageNo, pageSize);
            List<Map> xshtmxList = page.getResult();
            Map sum = xshtDao.findXshtHj(xshtId);
            String json = gson.toJson(xshtmxList);
            return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + json + ",\"footer\":[{\"cd.z\":\"合计:\",\"sbsl\":" + sum.get("sbsl") + ",\"hsje\":" + sum.get("hsje") + "}]}";
        } else
            return "{\"total\":0,\"rows\":[],\"footer\":[{\"cd.z\":\"合计:\",\"sbsl\":0,\"hsje\":0}]}";
    }

    @Transactional
    public String findXsmxFromKh(String xsxt_xsdh, String carNo, String weight, String bindNo, String bindNos, Long xshtId, int pageNo, int pageSize) {
        Gson gson = new Gson();
        Page<Map> page = xshtDao.findXsmxByKh(xsxt_xsdh, carNo, weight, bindNo, bindNos, xshtId, pageNo, pageSize);
        String json = gson.toJson(page.getResult());
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + json + "}";
    }

    public String findXshtmxDetailForXsmx(Long xshtId, int pageNo, int pageSize) {
        if (xshtId != null) {
            Gson gson = new Gson();
            Page<Map> page = this.xshtDao.findXshtmxListForXsmx(xshtId, pageNo, pageSize);
            List<Map> xshtmxList = page.getResult();
            Map sum = xshtDao.findXshtHj(xshtId);
            String json = gson.toJson(xshtmxList);
            return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + json + ",\"footer\":[{\"cd.z\":\"合计:\",\"sbsl\":" + sum.get("sbsl") + ",\"hsje\":" + sum.get("hsje") + "}]}";
        } else
            return "{\"total\":0,\"rows\":[],\"footer\":[{\"cd.z\":\"合计:\",\"sbsl\":0,\"hsje\":0}]}";
    }

    /**
     * 查询销售合同明细
     *
     * @param xshtId 售合合同ID
     * @return
     */
    public List<Map> findXshtmxDetailList(Long xshtId) {
        return this.xshtDao.findXshtmxList(xshtId, 1, -1).getResult();
    }

    /**
     * 保存销售合同信息
     *
     * @param xsht 销售合同
     * @return
     */
    @Transactional
    public Xsht saveXsht(Xsht xsht) throws SQLException {
        xsht.setHtbm(this.bmService.getHtbm("XSHT"));
        if (xsht.getSl() != null) {
            java.text.DecimalFormat df = new java.text.DecimalFormat("#0.0000");
            String sl = df.format(xsht.getSl());
            xsht.setSl(Double.parseDouble(sl));
        }
        if (xsht.getLl() != null) {
            java.text.DecimalFormat df = new java.text.DecimalFormat("#0.0000");
            String ll = df.format(xsht.getLl());
            xsht.setLl(Double.parseDouble(ll));
        }
        xsht.setZt("ybc");
        xsht.setCreateUser(YgService.getCurrentYg());
        xsht.setCreateOrgan(YgService.getCurrentYg().getZggs());
        xsht.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        xsht.setDateCreated(new Date());
        xsht.setLastUpdated(xsht.getDateCreated());
        if (xsht.getHtmxList() != null) {
            for (int i = 0; i < xsht.getHtmxList().size(); i++) {
                xsht.getHtmxList().get(i).setMxh(Long.valueOf(i + 1));
            }
        }
        this.xshtDao.save(xsht);
        return xsht;
    }

    /**
     * 修改销售合同信息
     *
     * @param xsht 销售合同
     * @return
     */
    @Transactional
    public void editXsht(Xsht xsht, String htbm) {
        xsht.setHtbm(htbm);
        if (xsht.getGspz() != null && xsht.getGspz().getId() == null) {
            xsht.setGspz(null);
        }
        if (null == xsht.getLj()) {
            xsht.setLj(null);
        } else {
            if (xsht.getLj().getId() == null) {
                xsht.setLj(null);
            }
        }

        if (null == xsht.getSbr()) {
            xsht.setSbr(YgService.getCurrentYg());
        } else {
            if (xsht.getSbr().getId() == null) {
                xsht.setSbr(YgService.getCurrentYg());
            }
        }

        if (null == xsht.getSbbm()) {
            xsht.setSbbm(null);
        } else {
            if (xsht.getSbbm().getId() == null) {
                xsht.setSbbm(null);
            }
        }

      /*  if (xsht.getSbbm() != null && xsht.getSbbm().getId() == null) {
            xsht.setSbbm(null);
        }*/
        if (xsht.getSl() != null) {
            java.text.DecimalFormat df = new java.text.DecimalFormat("#0.0000");
            String sl = df.format(xsht.getSl());
            xsht.setSl(Double.parseDouble(sl));
        }
        if (xsht.getLl() != null) {
            java.text.DecimalFormat df = new java.text.DecimalFormat("#0.0000");
            String ll = df.format(xsht.getLl());
            xsht.setLl(Double.parseDouble(ll));
        }
        if (xsht.getHtmxList() != null) {
            for (int i = 0; i < xsht.getHtmxList().size(); i++) {
                if (xsht.getHtmxList().get(i).getMxh() == null)
                    xsht.getHtmxList().get(i).setMxh(Long.valueOf(i + 1));
            }
        }
        xsht.setLastUpdated(new Date());
        this.xshtDao.save(xsht);
    }

    @Transactional
    public void transferXsht(Long xshtId) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, SQLException {
        Xsht xsht = this.xshtDao.get(xshtId);
        Cght cght = xsht.getZddhcght();
        if (cght != null) {
            List<Htmx> htmxList = cght.getHtmxList();
            List<Htmx> xshtmxList = new ArrayList<Htmx>();
            for (Htmx htmx : htmxList) {
                Htmx xshtmx = new Htmx();
                PropertyUtils.copyProperties(xshtmx, htmx);
                xshtmx.setId(null);
                java.text.DecimalFormat df = new java.text.DecimalFormat("#0.0000");
                Double ll = 0.0000d;
                if (cght.getWzjh() != null) {
                    Cgwzjh cgwzjh = this.cgwzjhDao.get(cght.getWzjh().getId());
                    ll = ((Zgwzjh) cgwzjh).getLrbl();
                } else
                    ll = cght.getLj().getLl();
                String bhsj = df.format(htmx.getBhsj() * (1 + ll / 100));
                xshtmx.setBhsj(Double.parseDouble(bhsj));
                String hsj = df.format(htmx.getHsj() * (1 + ll / 100));
                xshtmx.setHsj(Double.parseDouble(hsj));
                String hsje = df.format(Double.parseDouble(hsj) * htmx.getSbsl());
                xshtmx.setHsje(Double.parseDouble(hsje));
                xshtmxList.add(xshtmx);
            }
            xsht.setHtmxList(xshtmxList);
            this.xshtDao.save(xsht);
        }
    }


    /**
     * 删除销售合同信息
     *
     * @param id 销售合同ID
     * @return
     */
    @Transactional
    public Xsht deleteXsht(Long id) {
        Xsht xsht = this.xshtDao.get(id);
        xsht.setDeleted(1);
        xsht.setLastUpdated(new Date());
        this.xshtDao.save(xsht);
        this.xshtDao.deleteHtmx(id);
        return xsht;
    }

    /**
     * 删除销售合同信息
     *
     * @param id 销售合同ID
     * @return
     */
    @Transactional
    public void deleteXshtByCght(Long id) {
        List<Xsht> xshtList = this.xshtDao.findByProperty("zddhcght.id", id, null);
        for (Xsht xsht : xshtList) {
            xsht.setDeleted(1);
            xsht.setLastUpdated(new Date());
            this.xshtDao.save(xsht);
            this.xshtDao.deleteHtmx(xsht.getId());
        }
    }


    /**
     * 履约销售合同信息
     *
     * @param id 销售合同ID
     * @return
     */
    @Transactional
    public void agreeXsht(Long id) {
        Xsht xsht = this.xshtDao.get(id);
        xsht.setZt("yly");
        xsht.setLastUpdated(new Date());
        this.xshtDao.save(xsht);
    }

    /**
     * 终止销售合同信息
     *
     * @param id 销售合同ID
     * @return
     */
    @Transactional
    public void stopXsht(Long id) {
        Xsht xsht = this.xshtDao.get(id);
        xsht.setZt("yzz");
        xsht.setLastUpdated(new Date());
        this.xshtDao.save(xsht);
    }

    /**
     * 付款方式校验
     *
     * @param fkgs
     */
    public String checkFkgs(String fkgs) {
        return this.xshtDao.checkFkgs(fkgs);
    }

    public String checkJzjsFromXsht1(Long xshtId) {
        Xsht xsht = xshtDao.get(xshtId);
        Cght cght = this.cghtDao.get(xsht.getZddhcght().getId());
        String result = "";
        DecimalFormat df = new DecimalFormat("0.00");
        //    List<Map> jzjsList = this.jzjsDao.checkXshtHtJzjs(xshtId);
        List<Jzjs> jzjsList = this.jzjsDao.checkHtJzjs(xsht.getZddhcght().getId());
        if (null != jzjsList && jzjsList.size() > 0) {
            //1、这个采购合同额含税金额
            Double hthsje = cght.getHsze();
            Double sumSkje = 0.0;
            for (Jzjs hthk : jzjsList) {
                sumSkje += hthk.getJe();
            }
            Double d1 = CalcUtils.round(Double.valueOf(df.format(sumSkje)), 2);
            Double d2 = CalcUtils.round(Double.valueOf(df.format(hthsje)), 2);
            if (d1 < d2) {
                result = "此合同所需居中结算不足，还差" + df.format(d2 - d1) + "元，请确认！";
            }
        } else {
            result = "此合同未居中结算!";
        }
        return result;
    }

    @Transactional
    public Xsht tjXsht(Long id, Jydd jydd) {
        Xsht xsht = this.xshtDao.get(id);
        xsht.setZt("ysp");
        xsht.setLastUpdated(new Date());
        // 居中结算中存在赎货业务，如果不是赎货情况
        if (null == jydd.getDingdan_type() || !jydd.getDingdan_type().equals("1")) {
            List<Xsd> xsdList = xsht.getXsdList();
            for (Xsd xsd : xsdList) {
                xsd.setZt("ypk");
                this.ckshService.checkCksh(xsd);//出库操作减去库存数量
                if (!xsd.getXslx().equals("kcls")) {
                    xsd.setCwsh(true);
                    xsd.setShr(YgService.getCurrentYg());
                    xsd.setShsj(new Date());
                }
                List<Xsmx> xsmxList = xsd.getXsmxList();
                for (Xsmx xsmx : xsmxList) {
                    xsmx.setXscb(xsmx.getKh().getWsdj());
                    xsmx.setLastUpdated(new Date());
                }
                xsd.setLastUpdated(new Date());
                this.xsdDao.save(xsd);
            }
        }
        this.xshtDao.save(xsht);
        return xsht;
    }

    @Autowired
    public void setXshtDao(XshtDao xshtDao) {
        this.xshtDao = xshtDao;
    }

    @Autowired
    public void setBmService(BmService bmService) {
        this.bmService = bmService;
    }

    @Autowired
    public void setLjwhDao(LjwhDao ljwhDao) {
        this.ljwhDao = ljwhDao;
    }

    @Autowired
    public void setSjzdzService(SjzdzService sjzdzService) {
        this.sjzdzService = sjzdzService;
    }

    @Autowired
    public void setCgwzjhDao(CgwzjhDao cgwzjhDao) {
        this.cgwzjhDao = cgwzjhDao;
    }

    @Autowired
    public void setBgjhDao(BgjhDao bgjhDao) {
        this.bgjhDao = bgjhDao;
    }

    @Autowired
    public void setFkfsDao(FkfsDao fkfsDao) {
        this.fkfsDao = fkfsDao;
    }

    @Autowired
    public void setJzjsDao(JzjsDao jzjsDao) {
        this.jzjsDao = jzjsDao;
    }

    @Autowired
    public void setXsdDao(XsdDao xsdDao) {
        this.xsdDao = xsdDao;
    }

    @Autowired
    public void setCghtDao(CghtDao cghtDao) {
        this.cghtDao = cghtDao;
    }

    @Autowired
    public void setCkshService(CkshService ckshService) {
        this.ckshService = ckshService;
    }
}
